<?php
namespace app\index\controller;
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods:POST,GET");

use app\common\controller\HomeBase;
use think\Db;

class Luntanmain extends HomeBase
{
	protected function _initialize(){
		parent::_initialize();
	}


	/**
	*论坛首页栏目(只显示八个) 
	**/
	public function mainCategory()
	{
		$categoryList = Db('category')->page(1,8)->select();
		//判断
		if($categoryList){
            $this->apiSuccess($categoryList);
        }else{
            $this->apiError('没有查到信息');
        }
	}


	/**
	*论坛首页栏目(显示全部栏目) 
	**/
	public function mainAllcategory()
	{
		$categoryList = Db('category')->select();
		//判断
		if($categoryList){
            $this->apiSuccess($categoryList);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*论坛首页公告
	**/

	public  function mainNotice()
	{
		$noticeList = Db('notice')->where('status',1)->select();
		//判断
		if($noticeList){
            $this->apiSuccess($noticeList);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*论坛首页搜索
	*@param  int page 		  分页的第几页
	*@param  int pagelimite   每页显示几条
	**/
	public  function search()
	{
		//获取表单提交的查询title
		$title = $_POST['title'];
		//接收数据
		$page = (isset($_POST['page']) ? $_POST['page']:1);
		$pagelimit = (isset($_POST['pagelimit']) ? $_POST['pagelimit']:5);

		$title_like['title']=['like',"%{$title}%"];
		$topList = Db('article')->where('is_top',1)->where($title_like)->order('create_time','desc')->page($page,$pagelimit)->select();
		$noTopList = Db('article')->where('is_top',0)->where($title_like)->order('create_time','desc')->page($page,$pagelimit)->select();
		$postList1 = array_merge($topList,$noTopList);


		$List = $this->doShow($postList1);
		//获取页面总页数
		$count = count($List);
		$pagecount = ceil($count/$pagelimit);
		// $List['pagecount'] = $pagecount;
		$list['group'] = $List;
		$list['pagecount'] = $pagecount;
		//判断
		if($list){
            $this->apiSuccess($list);
        }else{
            $this->apiError('没有查到信息');
        }
	}


	/**
	*公共方法
	*@param   $arr 		  数组
	**/
	public function doShow($arr)
	{
		//查询用户名与头像信息	
		$i = 0;
		for (; $i < count($arr); $i++) { 
			$id = $arr[$i]['uid'];
			$is_user = $arr[$i]['is_user'];
			if($is_user == 1){
				$userInfo = Db('user')->where('id',$id)->select();
				$arr[$i]['headImg'] = $userInfo[0]['headImg'];
				$arr[$i]['userName'] = $userInfo[0]['name'];
			}elseif($is_user == 2){
				$userInfo = Db('admin_user')->where('id',$id)->select();
				$arr[$i]['headImg'] = $userInfo[0]['headImg'];
				$arr[$i]['userName'] = $userInfo[0]['username'];
			}
			$arr1[] = $arr[$i];			
		}
		

		//将数组中图片给unserialize
		foreach ($arr1 as $val) {
			$photo = unserialize($val['photo']);		
			$val['photo'] = $photo;
			$List[] = $val;
		}
		return $List;
	}

	/**
	*最新帖子
	*@param  int page 		  分页的第几页
	*@param  int pagelimite   每页显示几条
	**/
	public function newList()
	{
		//接收数据
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:10);

		//查询 分别查询置顶与非置顶,然后合并两个数组,置顶的自动就排在前面
		$topList = Db('article')->where('is_top',1)->where('status',1)->order('create_time','desc')->page($page,$pagelimit)->select();
		$noTopList = Db('article')->where('is_top',0)->where('status',1)->order('create_time','desc')->page($page,$pagelimit)->select();
		$newList1 = array_merge($topList,$noTopList);

		$List = $this->doShow($newList1);
		//获取页面总页数
		$count = count($List);
		$pagecount = ceil($count/$pagelimit);
		$newlist['group'] = $List;
		$newlist['pagecount'] = $pagecount;
		//判断
		if($newlist){
            $this->apiSuccess($newlist);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	/**
	*最热帖子
	*@param  int page 		 分页的第几页
	*@param  int pagelimit   每页显示几条
	**/

	public function hotList()
	{

		//接收表单穿过来的数据
		$page = (isset($_GET['page']) ? $_GET['page']:1);
		$pagelimit = (isset($_GET['pagelimit']) ? $_GET['pagelimit']:5);

		//查询  分别查询置顶与非置顶,然后合并两个数组,置顶的自动就排在前面
		$topList = Db('article')->where('is_top',1)->where('status',1)->order('reading','desc')->page($page,$pagelimit)->select();
		$noTopList = Db('article')->where('is_top',0)->where('status',1)->order('reading','desc')->page($page,$pagelimit)->select();
		$hotList1 = array_merge($topList,$noTopList);

		$List = $this->doShow($hotList1);

		//获取页面总页数
		$count = count($List);
		$pagecount = ceil($count/$pagelimit);
		// $List['pagecount'] = $pagecount;
		$list['group'] = $List;
		$list['pagecount'] = $pagecount;
		//判断
		if($list){
            $this->apiSuccess($list);
        }else{
            $this->apiError('没有查到信息');
        }
	}

	

	/**
	*帖子的添加
	*@param  int     uid	   用户的uid
	*@param  int     cid	   发帖栏目类型
	**/
	public function postAdd()
	{		
		$uid = $_POST['uid'];
		$cid = $_POST['cid'];

		//获得用户名字
		$userInfo =  Db('user')->where('id',$uid)->select();
		$author = $userInfo[0]['name'];
		//获得表单提交的数据
		$title = $_POST['title'];
		$content = $_POST['content'];
		//实体化表单数据
		$title = htmlspecialchars($title);
		$content = htmlspecialchars($content);

		// 获取表单上传文件
		$files = request()->file('file');
		if ($files) {
			foreach($files as $file){
				// 移动到框架应用根目录/public/uploads/ 目录下
				$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
				if($info){
					// 成功上传后 将成功的文件名存入数组
					$arr[] = $info->getFilename('图片添加失败,请重试');
					
				}else{
					// 上传失败获取错误信息
					echo $file->getError();
				}
			}
			//遍历拼接数组名字
			foreach($arr as $photoName){
				$data = date('Ymd',strtotime('now'));
				$path = '/uploads/'.$data.'/'.$photoName;
				$photo[] = $path;
			}

			//将数组序列化之后存入数据库
			$photo = serialize($photo);
		}else{
			$photo = '';
		}
		$create_time = date('Y-m-d H:i:s',strtotime('now'));
		$data = [
			'uid'=>$uid,
			'cid'=>$cid,
			'title'=>$title,
			'content'=>$content,
			'author'=>$author,
			'photo'=>$photo,
			'create_time'=>$create_time
		];

		$add = Db('article')->insert($data);

		if($add){
			$this->apiSuccess('','添加成功');
		}else{
			$this->apiError('添加失败,请重试');
		}

	}

}